/**
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2007，所有权利保留。
 * 
 * 项目名：	rumba
 * 文件名：	SubscriberMgr.java
 * 模块说明：	
 * 修改历史：
 * Oct 30, 2007 - lxm - 创建。
 */
package com.hd123.rumba.mscb.subscriber;

import java.util.ArrayList;
import java.util.List;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.type.Type;

import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.runtime.IManager;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.runtime.biz.BManager;
import com.hd123.rumba.user.CRole;
import com.hd123.rumba.user.CUser;
import com.hd123.rumba.user.IRole;
import com.hd123.rumba.user.IRoleMgr;
import com.hd123.rumba.user.IUser;
import com.hd123.rumba.user.IUserMgr;
import com.hd123.rumba.user.IUserRole;

/**
 * 订阅者 | 管理员 | 实现类
 * <p>
 * 
 * 适用于rumba提供的IUser和IRole。
 * 
 * @author lxm
 * 
 */
public class SubscriberMgr extends BManager implements ISubscriberMgr {

  @Override
  public IManager getPerzMgr() throws BusinessException {
    return null;
  }

  public Subscriber toSubscriber(Object nature) throws BusinessException {
    this.checkParameterNotNull(nature, "nature");

    Subscriber sub = new Subscriber();
    sub.setMgrClassName(SubscriberMgr.class.getName());
    if (nature instanceof IUser) {
      sub.setType_(CSubscriber.VALUE_TYPE_USER);
      sub.setId(((IUser) nature).getUuid());
    } else if (nature instanceof IRole) {
      sub.setType_(CSubscriber.VALUE_TYPE_ROLE);
      sub.setId(((IRole) nature).getUuid());
    } else
      throw new BusinessException("传入的参数nature不支持的数据类型。");
    return null;
  }

  public Object toNature(Subscriber subscriber) throws BusinessException, HibernateException {
    this.checkParameterNotNull(subscriber, "subscriber");

    if (CSubscriber.VALUE_TYPE_USER.equals(subscriber)) {
      IUser user = getUserMgr().getUser(subscriber.getId());
      return user;
    } else if (CSubscriber.VALUE_TYPE_ROLE.equals(subscriber)) {
      IRole role = getRoleMgr().getRole(subscriber.getId());
      return role;
    } else
      return null;
  }

  public List<Subscriber> getUsersByRole(Subscriber role) throws BusinessException,
      HibernateException {
    this.checkParameterNotNull(role, "role");

    IRole nrole = (IRole) this.toNature(role);
    if (nrole == null)
      throw new BusinessException("找不到指定的" + CSubscriber.CAPTION_TYPE_ROLE + "。");

    List<Subscriber> users = new ArrayList<Subscriber>();
    if (CRole.VALUE_STATE_DELETED.equals(nrole.getState()))
      return users;
    List<IUser> nusers = getRoleMgr().createUsersList(nrole);
    for (IUser nuser : nusers) {
      Subscriber user = this.toSubscriber(nuser);
      users.add(user);
    }
    return users;
  }

  public List<Subscriber> getRolesByUser(Subscriber user) throws BusinessException,
      HibernateException {
    this.checkParameterNotNull(user, "user");

    IUser nuser = (IUser) this.toNature(user);
    if (nuser == null)
      throw new BusinessException("找不到指定的" + CSubscriber.CAPTION_TYPE_USER + "。");

    List<Subscriber> roles = new ArrayList<Subscriber>();
    if (CUser.VALUE_STATE_DELETED.equals(nuser.getState()))
      return roles;
    List<IRole> nroles = getUserMgr().createRolesList(nuser);
    for (IRole nrole : nroles) {
      Subscriber role = this.toSubscriber(nrole);
      roles.add(role);
    }
    return roles;
  }

  public boolean isValid(Subscriber subscriber) throws BusinessException, HibernateException {
    this.checkParameterNotNull(subscriber, "subscriber");

    if (CSubscriber.VALUE_TYPE_USER.equals(subscriber)) {
      IUser nuser = (IUser) this.toNature(subscriber);
      return !CUser.VALUE_STATE_DELETED.equals(nuser.getState());
    } else if (CSubscriber.VALUE_TYPE_ROLE.equals(subscriber)) {
      IRole nrole = (IRole) this.toNature(subscriber);
      return !CRole.VALUE_STATE_DELETED.equals(nrole.getState());
    } else
      return false;
  }

  public boolean relative(Subscriber role, Subscriber user) throws BusinessException,
      HibernateException {
    this.checkParameterNotNull(role, "role");
    this.checkParameterNotNull(user, "user");

    List list = getSession().find(
        "select count(ur) from " + IUserRole.class.getName() + " ur"
            + " where ur.user.uuid = ? and ur.role.uuid = ?"
            + " and ur.user.state <> ? and ur.role.state <> ?", new Object[] {
            user.getId(), role.getId(), CUser.VALUE_STATE_DELETED, CRole.VALUE_STATE_DELETED },
        new Type[] {
            Hibernate.STRING, Hibernate.STRING, Hibernate.INTEGER, Hibernate.INTEGER });
    Integer cnt = (Integer) list.get(0);
    return cnt.intValue() != 0;
  }

  private IUserMgr userMgr = null;
  private IRoleMgr roleMgr = null;

  private IUserMgr getUserMgr() throws BusinessException {
    if (userMgr == null)
      userMgr = (IUserMgr) NamingMgr.getInstance().lookupMgr(IUserMgr.class);
    return userMgr;
  }

  private IRoleMgr getRoleMgr() throws BusinessException {
    if (roleMgr == null)
      roleMgr = (IRoleMgr) NamingMgr.getInstance().lookupMgr(IRoleMgr.class);
    return roleMgr;
  }

}
